import { Message, MessageBox } from 'element-ui'
import { isEmpty, isNotEmpty } from '../validate'

/**
 * 构建消息
 * @param content 内容
 */
function buildMessage(content: string | string[]) {
  let message = ''
  if (isNotEmpty(content)) {
    if (Array.isArray(content)) {
      message = content.join('<br />')
    } else {
      message = content
    }
  }
  return message
}

/**
 * 显示消息
 * @param content 内容
 * @param type 类型
 * @param title 标题
 * @param force 是否强制提示
 */
export const showMessage = (
  content: string | string[],
  type: 'info' | 'success' | 'warning' | 'error' = 'info',
  title: string = '消息',
  force: boolean = false
) => {
  const message = buildMessage(content)
  if (isNotEmpty(message)) {
    if (force) {
      MessageBox.alert(message, title, {
        dangerouslyUseHTMLString: true,
        type: type,
        showConfirmButton: false,
        closeOnClickModal: true,
        closeOnPressEscape: true,
        callback: function() {}
      })
    } else {
      Message({
        showClose: true,
        message,
        dangerouslyUseHTMLString: true,
        type,
        duration: 5 * 1000
      })
    }
  }
}

/**
 * 消息提示
 * @param content 内容
 * @param type 类型
 * @param title 标题
 */
export const showAlert = (
  content: string | string[],
  type: 'info' | 'success' | 'warning' | 'error' = 'info',
  title: string = '提示'
) => {
  const message = buildMessage(content)
  return MessageBox.alert(message, title, {
    dangerouslyUseHTMLString: true,
    type,
    confirmButtonClass: 'el-icon-antd-check',
    confirmButtonText: '是'
  })
}

/**
 * 确认提示
 * @param content 内容
 * @param type 类型
 * @param title 标题
 */
export const showConfirm = (
  content: string | string[],
  type: 'info' | 'success' | 'warning' | 'error' = 'info',
  title: string = '请确认 ...'
) => {
  const message = buildMessage(content)
  return MessageBox.confirm(message, title, {
    dangerouslyUseHTMLString: true,
    type,
    confirmButtonClass: 'el-icon-antd-check',
    confirmButtonText: '是',
    showCancelButton: false
  })
}

/**
 * 提示输入
 * @param content 内容
 * @param title 标题
 */
export const showPrompt = (
  content: string | string[],
  title: string = '请输入 ...'
) => {
  const message = buildMessage(content)
  return MessageBox.prompt(message, title, {
    dangerouslyUseHTMLString: true,
    confirmButtonClass: 'el-icon-antd-check',
    confirmButtonText: '提交',
    showCancelButton: false,
    inputValidator: (value) => {
      if (isEmpty(value)) {
        return '请输入提交信息'
      }
      return true
    }
  })
}
